package org.goyo.main.services.storage.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.goyo.common.baseBean.Constants;
import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.common.utils.DateUtils;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.dao.report.ISysReport;
import org.goyo.main.domain.dao.sale.IDispatchDao;
import org.goyo.main.domain.dao.sale.ISaleOrderDao;
import org.goyo.main.domain.dao.storage.IGroupMaterialDao;
import org.goyo.main.domain.entity.report.InventoryReport;
import org.goyo.main.domain.entity.storage.GroupMaterial;
import org.goyo.main.domain.entity.storage.GroupMaterialEntry;
import org.goyo.main.domain.entity.storage.PurInStorage;
import org.goyo.main.domain.entity.storage.PurInStorageEntry;
import org.goyo.main.domain.entity.system.SysUser;
import org.goyo.main.services.BaseService;
import org.goyo.main.services.storage.IGroupMaterialService;
import org.springframework.stereotype.Service;
@Service
@Transactional
public class GroupMaterialServiceImpl  extends BaseService implements IGroupMaterialService{
	@Resource
	private IGroupMaterialDao groupMaterialDao;
	@Resource
	private ISaleOrderDao saleOrderDao;
	@Resource
	private IDispatchDao dispatchDao;
	@Resource
	private ISysReport sysReport;

	@Override
	public Pager<GroupMaterial> findByCondition(String where, Pager pager)
			throws DBException, ServiceException {
		
		return  groupMaterialDao.findByCond(where, pager);
	}

	@Override
	public GroupMaterial saveOrUpdateInfo(GroupMaterial info, Object[] uuids,SysUser user)
			throws DBException, ServiceException {
		String uuid = null;
		//保存用户数据
		info.setStatus(Constants.SAVESTATUS);
		info.setAlterDate(DateUtils.getCurrentDate());
		info.setAlterUser(user);
		if(info.getUuid() == null || "".equals(info.getUuid())){
			info.setUuid(null);
			info.setCreateDate(DateUtils.getCurrentDate());
			info.setCreateUser(user);
			uuid = groupMaterialDao.save(info);
//			alterBillNumber(GroupMaterial.BIZTYPE, info.getNumber());
		}else{
			groupMaterialDao.update(info);
			uuid = info.getUuid();
		}
		//删除数据
		if(uuids != null && uuids[0] != null){
			groupMaterialDao.delBatch(GroupMaterialEntry.class, "uuid", uuids);
		}
		info.setUuid(uuid);
		return info;
	}

	@Override
	public List<GroupMaterialEntry> findByParentId(String parentId) throws DBException,
			ServiceException {
		return groupMaterialDao.findByParentId(parentId);
	}
	
	@Override
	public void delByUuids(Object[] uuids) throws DBException, ServiceException {
		//批量删除
		for(Object uuid : uuids){
			GroupMaterial info = groupMaterialDao.findByUuid(GroupMaterial.class, uuid.toString());
			groupMaterialDao.delete(info);
		}
	}

	@Override
	public GroupMaterial submitByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		GroupMaterial info = null;
		//批量提交
		for(Object uuid : uuids){
			info = groupMaterialDao.findByUuid(GroupMaterial.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setSubmitDate(DateUtils.getCurrentDate());
			info.setSubmitUser(user);
			info.setStatus(Constants.SUBMITTATUS);
			groupMaterialDao.update(info);
		}
		return info;
	}

	@Override
	public GroupMaterial auditByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		GroupMaterial info = null;
		//批量审核
		for(Object uuid : uuids){
			info = groupMaterialDao.findByUuid(GroupMaterial.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setAuditDate(DateUtils.getCurrentDate());
			info.setAuditUser(user);
			info.setStatus(Constants.AUDITSTATUS);
			List<GroupMaterialEntry> entrys = info.getEntrys();
			groupMaterialDao.update(info);
		}
		return info;
	}
	
	/**
	 * 更新库存报表
	 * @author chenchuan
	 * 2014年12月7日
	 */
	public void updateInventroyReport(GroupMaterial info) throws DBException{
		List<GroupMaterialEntry> entrys = info.getEntrys();
		for(GroupMaterialEntry entry : entrys){
			String materialId = entry.getMaterial().getUuid();
			InventoryReport inventory = sysReport.findByMaterialId(materialId);
			//获取
			Long qty = groupMaterialDao.getSumQty(materialId);
			if(inventory == null){
				inventory = new InventoryReport();
			}
			inventory.setMaterial(entry.getMaterial());
			inventory.setCheckQty(qty);
			sysReport.saveOrUpdate(inventory);
		}
	}
	
	@Override
	public GroupMaterial unAuditByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		GroupMaterial info = null;
		//批量反审核
		for(Object uuid : uuids){
			info = groupMaterialDao.findByUuid(GroupMaterial.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setAuditDate(null);
			info.setAuditUser(null);
			info.setSubmitUser(null);
			info.setSubmitDate(null);
			info.setStatus(Constants.SAVESTATUS);
			groupMaterialDao.update(info);
		}
		return info;
	}

	@Override
	public GroupMaterial findByUuuid(String uuid) throws DBException,
			ServiceException {
		GroupMaterial info = groupMaterialDao.findByUuid(GroupMaterial.class, uuid);
		return info;
	}
}
